module majiang.Views {
    /**
     * HupaiArea
     */
    export class HupaiArea extends eui.Component {
        //eui
        public majiangGroup:eui.Group;

        //data

        constructor() {
            super();
        }

        /**
         * gameStart
         * 新一局游戏开始
         */
        public gameStart() {
            //初始化各项数据
            this.initHupaiArea();

            majiang.Controllers.GuideMain.loadGuideScript(1,this.majiangGroup.getChildAt(1));
        }
        /**
         * 重置胡牌区所有牌
         * 一般作为 游戏开始时 胡牌之后
         */
        public initHupaiArea() {
            this.removeAllMajing();
            this.randomAllMajiangChi();
            this.addAllMajiang();
            this.hupaiCheck();
        }

        /**
         * 添加麻将到胡牌区
         */
        private addAllMajiang(){
            for (var i = 0; i < 11; i++) {
                var element = majiang.Datas.Playing.majiangChi.shift();
                this.addMajiang(element,false);
            }
        }

        /**
         * 将所有麻将移除到麻将对象池
         */
        private removeAllMajing() {
            var index = this.majiangGroup.numChildren;
            for (var i = 0; i < index; i++) {
                var element = <majiang.Views.MajiangBtn> this.majiangGroup.getChildAt(0);
                this.removeMajiang(element,false);
            }
        }
        /**
         * 随机当前所有牌
         */
        private randomAllMajiangChi(){
            //重置胡牌区11个麻将牌
            for (var i = 0; i < majiang.Datas.Playing.majiangChi.length; i++) {
                var element:majiang.Views.MajiangBtn = majiang.Datas.Playing.majiangChi[i];
                element.setMajingpai(11,"li","xuan");
            }
        }

        /**
         * removeMajiang
         * 移除一张麻将牌
         */
        public removeMajiang(pai:majiang.Views.MajiangBtn,isCheck = true) {
            this.majiangGroup.removeChild(pai);

            //回收到麻将池中
            majiang.Datas.Playing.majiangChi.push(pai);

            if(isCheck){
                this.hupaiCheck();
            }
        }

        /**
         * addMajiang
         * 在胡牌区 增加一张牌
         */
        public addMajiang(pai:majiang.Views.MajiangBtn,isCheck:boolean = true) {
            pai.setForArea("hu");

            this.addMajiangPlace(pai);

            if(isCheck){
                this.hupaiCheck();
            }
        }

        /**
         * 给放入的牌 安排合适的位置
         */
        private addMajiangPlace(pai:majiang.Views.MajiangBtn){
            //没有其他牌的时候 直接放入
            if(this.majiangGroup.numChildren === 0){
                this.majiangGroup.addChild(pai);
                pai.x = 0;
                // console.log(pai.paiId,pai.x);
                return
            }

            //比某一个小 放它前面
            var isFindIndex:boolean = false;
            var findIndex:number;
            for (var index = 0; index < this.majiangGroup.numChildren; index++) {
                var element:majiang.Views.MajiangBtn = <majiang.Views.MajiangBtn> this.majiangGroup.getChildAt(index);
                if(pai.paiId <= element.paiId && !isFindIndex){
                    findIndex = index;
                    isFindIndex = true;
                }

                if(isFindIndex){
                    //找到位置 后面的x坐标变更
                    element.x = pai.width * pai.scaleX * (index + 1);
                }
                // console.log(element.paiId,element.x);
            }

            if(isFindIndex){
                this.majiangGroup.addChildAt(pai,findIndex);
                pai.x = pai.width * pai.scaleX * findIndex;
                // console.log(pai.paiId,pai.x);
                return
            }

            
            //比所有都大 放最后
            this.majiangGroup.addChildAt(pai,this.majiangGroup.numChildren);
            pai.x = pai.width * pai.scaleX * (this.majiangGroup.numChildren + 1);
            // console.log(pai.paiId,pai.x);
        }

        /**
         * hupai
         */
        public hupai() {
            // var max:number = this.majiangGroup.numChildren;
            for(var i = 0;i < this.majiangGroup.numChildren; i ++){
                var element = <majiang.Views.MajiangBtn> this.majiangGroup.getChildAt(i);
                element.hupai();
            }
        }


        private paixing:any;
        /**
         * 获取当前牌型的数量
         */
        private getPaixing():any {
            if(this.paixing == null){
                this.paixing = {};
                for (var i = 0; i < this.majiangGroup.numChildren; i++) {
                    var element = <majiang.Views.MajiangBtn> this.majiangGroup.getChildAt(i);
                    if(this.paixing[element.paiId]){
                        this.paixing[element.paiId] ++;
                    }else{
                        this.paixing[element.paiId] = 1;
                    }
                }
            }

            var obj:any = {};
            for(var x in this.paixing){
                obj[x] = this.paixing[x];
            }
            return obj;
        }
        /**
         * hupaiCheck
         * 当胡牌区达到11张牌时 检查是否胡牌
         */
        public hupaiCheck() {
            //胡牌区 牌数不为11时 返回
            if(this.majiangGroup.numChildren !== 11){
                majiang.Controllers.Play.isCanHupai(false);
                return
            }

            this.paixing = null;

            if(this.isCanDuizi()){
                //是暗对子
                majiang.Controllers.Play.isCanHupai(true);
                return
            }

            if(this.isCanPinghu()){
                //能胡牌
                majiang.Controllers.Play.isCanHupai(true);
                return
            }

            //不能胡牌
            majiang.Controllers.Play.isCanHupai(false);
        }

        //是不是暗对子
        private isCanDuizi():boolean{
            var obj:any = this.getPaixing();
            var duiziIndex:number = 0;
            var shunziIndex:number = 0;
            for(var i in obj){
                if(obj[i] === 2){
                    duiziIndex += 1;
                }else if(obj[i] === 4){
                    duiziIndex += 2;
                }else if(obj[i] === 3){
                    shunziIndex += 1;
                }
            }

            //检查完毕 删除obj
            obj = null;

            if(duiziIndex === 4  && shunziIndex === 1){
                
                return true;
            }else{

                return false;
            }
        }

        // 是否能胡牌
        private isCanPinghu():boolean{
            //检查是否有对子
            var obj:any;
            obj = this.getPaixing();

            //找对子
            var duiziIndex:number = 0;
            for(var i in obj){
                if(obj[i] >= 2){
                    duiziIndex ++;
                }
            }

            //先判断2个对子情况
            if(duiziIndex >= 2){
                //有2对及以上 先判断2对情况
                if(this.check2dui(obj)){
                    return true
                }
            }
            
            //1个对子情况
            if(duiziIndex >= 1){
                if(this.check1dui(obj)){
                    //除去其中1对后 能3张
                    return true
                }
            }

            //没有对子 不能胡牌 规则没有十三幺
            return false
        }

        /**
         * 去掉1对后 再使用check1dui
         */
        private check2dui(obj:any):boolean{
            for(var i in obj){
                if(obj[i] >= 2){
                    obj[i] -= 2;
                    if(this.check1dui(obj)){
                        //剩余牌 能3张
                        return true;
                    }else{
                        obj[i] += 2;
                    }
                }
            }
            
            return false;
        }
        /**
         * 检查 1对子时 能否胡
         * 除去其中1对 再放入 检查3张中
         */
        private check1dui(obj:any):boolean{
            for(var i in obj){
                if(obj[i] >= 2){
                    obj[i] -= 2;
                    if(this.check3zhang(obj)){
                        //剩余牌 能3张
                        return true;
                    }else{
                        obj[i] += 2;
                    }
                }
            }
            
            return false;
        }

        /**
         * 检查剩下的牌能否全是顺子或3张
         */
        private check3zhang(checkObj:any):boolean {
            var obj:any = {};
            for(var b in checkObj){
                obj[b] = checkObj[b];
            }

            //由于obj是顺序添加 只检查后续2个有没有就行了
            for(var i in obj){
                if(obj[i] !== 0){                    
                    var max:number = obj[i];
                    
                    var next1:string = parseInt(i) + 1 + "";
                    var next2:string = parseInt(i) + 2 + "";
                    for (var index = 0; index < max; index++) {
                        if(obj[i] > 0 && obj[next1] > 0 && obj[next2] > 0){
                            obj[i] --;
                            obj[next1] --;
                            obj[next2] --;
                        }        
                    }
                }

                if(obj[i] === 3){
                    obj[i] -= 3;
                }

                if(obj[i] !== 0){
                    // console.log("有单张 check3zhang:",obj);
                    return false
                }
            }

            return true
        }
        

        /**
         * getHupaiFanshu
         * 计算胡牌番数
         */
        public getHupaiFanshu():any {
            //默认开启3种牌型
            var isQingyise:boolean = true;
            var isDaiyao:boolean = true;
            var isJiangdui:boolean = true;

            var duizi:number = 0;
            var sanzhang:number = 0;
            var gang:number = 0;
            var fanshu:number = 0;
            var paixingArr:Array<string> = [];

            for(var i in this.paixing){
                if(this.paixing[i] === 2){
                    duizi ++;
                    if(parseInt(i)%10 !== 1 && parseInt(i)%10 !== 9){
                        isDaiyao = false;
                    }
                }else if(this.paixing[i] === 4){
                    gang ++;
                    if(parseInt(i)%10 !== 1 && parseInt(i)%10 !== 9){
                        isDaiyao = false;
                    }
                }else if(this.paixing[i] === 3){
                    sanzhang ++;
                }
            }

            //保存当前麻将牌
            
            for(var index:number=0;index < this.majiangGroup.numChildren ; index++){
                var element = <majiang.Views.MajiangBtn> this.majiangGroup.getChildAt(index);
                //判断清一色
                var color:number;
                if(!color){
                    color = Math.floor(element.paiId/10);
                }

                if(Math.floor(element.paiId/10) != color && isQingyise){
                    isQingyise = false;
                }

                //判断带幺
                if(element.paiId%10 >= 4 && element.paiId%10 <= 6 && isDaiyao){
                    //有456 绝对不是带幺
                    isDaiyao = false;
                }else{
                    //没有456的情况
                    if(duizi + gang*2 === 4){
                        if(element.paiId%10 !==1 && element.paiId%10 !== 9){
                            isDaiyao = false;
                        }
                    }else{

                    }
                }

                //判断将对
                if(element.paiId%10 !== 2 && element.paiId%10 !== 5 && element.paiId%10 !== 8 && isJiangdui){
                    isJiangdui = false;
                }
            }

            //开始 检查牌型 计算番数
            //大对子
            if(duizi === 1 && sanzhang === 3){
                fanshu += 2;
                paixingArr.push("dadui");
            }

            //清一色
            if(isQingyise){
                fanshu += 4;
                paixingArr.push("qingyise");
            }
            
            //暗对
            if(duizi === 4 && sanzhang === 1){
                fanshu += 4;
                paixingArr.push("andui");
            }

            //龙对
            if(gang === 1 && duizi === 2 && sanzhang === 1){
                fanshu += 8
                paixingArr.push("longdui");
            }

            //双龙对
            if(gang === 2 && sanzhang === 1){
                fanshu += 16
                paixingArr.push("shuanglongdui");
            }

            //带幺
            if(isDaiyao){
                fanshu += 8;
                paixingArr.push("daiyao");
            }

            //将对
            if(isJiangdui){
                fanshu += 8;
                paixingArr.push("jiangdui");
            }

            //平胡
            if(fanshu === 0){
                fanshu = 1;
                paixingArr.push("pinghu");
            }

            //带根
            if(gang > 0){
                fanshu = fanshu * (gang * 2);
                paixingArr.push("daigen");
            }

            // console.log("番数：" + fanshu , "牌型:" , paixingArr);
            return {fanshu:fanshu,paixingArr:paixingArr};
        }
    }
}